package com.chuangjiangx.member.basic.web.controller;

import com.chuangjiangx.commons.response.Response;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.commons.wx.jssdk.WXJssdkCardSign;
import com.chuangjiangx.member.coupon.ddd.application.MbrCouponApplication;
import com.chuangjiangx.member.basic.ddd.application.ShareApplication;
import com.chuangjiangx.member.basic.ddd.query.MerchantInfoQuery;
import com.chuangjiangx.member.basic.ddd.domain.model.msg.WXShare;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 微信浏览器分享：自定义分享内容链接
 */
@RestController
@RequestMapping("/h5/wx-js-sdk")
@Api(tags = "签名",description = "签名接口")
public class WXJsSdkController {

    @Autowired
    private ShareApplication shareApplication;

    @Autowired
    private MbrCouponApplication mbrCouponApplication;

    @Autowired
    private MerchantInfoQuery merchantInfoQuery;

//    @Value("${merchant.web.domain:''}")
//    private String merchantDomain;
//
//    @Value("#{configProperties['wxPay.sqURL.T']}")
//    private String oauth2ReqHead;
//
//    @Value("${wx.public.appId}")
//    private String appId;

    /**
     * js-sdk签名
     * 必须是微信浏览器
     */
    @RequestMapping(value = "/sign",method = RequestMethod.GET)
    public Response sign(
            HttpServletRequest request,
            @RequestParam String url,
            @RequestParam Long merchantId) {

        //获取浏览器类型
        String ua = request.getHeader("User-Agent");

        if (ua.contains("MicroMessenger")) {
            WXShare wxShare = shareApplication.wxShare(merchantId, url);
//            MerchantInfoDTO merchant = merchantInfoQuery.searchMerchantInfoForH5(merchantId);
//            wxShare.setTitle(merchant.getName());
//            wxShare.setDesc(merchant.getName());
//            wxShare.setImgUrl(merchant.getLogoUrl());
//            wxShare.setLink("/h5/entry?merchantId="+merchantId);
            return ResponseUtils.success(wxShare);
        } else {
            return ResponseUtils.error("", "请在微信中访问");
        }
    }

    /**
     * js-sdk签名
     * 必须是微信浏览器
     */
    @RequestMapping(value = "/card-sign",method = RequestMethod.GET)
    @ApiOperation(value = "获取卡券签名", notes = "获取卡券签名")
    public Response cardSign(
            HttpServletRequest request,
            @RequestParam String wxCardId,
            @RequestParam String code,
            @RequestParam Long merchantId) {

        //获取浏览器类型
        String ua = request.getHeader("User-Agent");
        WXJssdkCardSign wxJssdkCardSign = mbrCouponApplication.wxGetCard(merchantId, wxCardId, code);
        return ResponseUtils.success(wxJssdkCardSign);
    }
//    /**
//     * 分享链接地址：h5授权链接地址
//     */
//    @RequestMapping("/h5link")
//    public Response getH5Link() {
//        MbrUserContext memeberValue = (MbrUserContext) CurrentThreadContext.getCurrentUser();
//        if (memeberValue == null) {
//            return ResponseUtils.error("", "访问错误");
//        }
//        String domain = System.getProperty("merchant.web.domain");
//        if (StringUtils.isEmpty(domain)) {
//            throw new BaseException("00000", "会员H5域名未配置");
//        }
//        String url = oauth2ReqHead + memeberValue.getMerchantWxAppid() + "&redirect_uri=" +
//                domain + "/api/wx-public/member-h5-auth-redirect&response_type=code&scope=snsapi_userinfo&state="
//                + memeberValue.getMerchantId() + "&component_appid=" + appId + "#wechat_redirect";
//        H5LinkDataResponse response = new H5LinkDataResponse();
//        response.setUrl(url);
//        return ResponseUtils.success("urlInfo", response);
//    }

}
